package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.domain.SmsHomeRecommendSubject;
import com.itheima.mapper.SmsHomeRecommendSubjectMapper;
import com.itheima.service.SmsHomeRecommendSubjectService;
import com.itheima.util.PageBean;
import com.itheima.util.PageUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 首页推荐专题表 服务实现类
 * </p>
 *
 * @author zhuguo
 * @since 2022-11-10
 */
@DubboService
public class SmsHomeRecommendSubjectServiceImpl extends ServiceImpl<SmsHomeRecommendSubjectMapper, SmsHomeRecommendSubject> implements SmsHomeRecommendSubjectService {
    @Autowired
    private SmsHomeRecommendSubjectMapper subjectMapper;

    @Override
    public PageBean list(Integer pageNum, Integer pageSize, String subjectName, Integer recommendStatus) {
        Page<SmsHomeRecommendSubject> pageInfo = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<SmsHomeRecommendSubject> queryWrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(subjectName)){
            queryWrapper.like(SmsHomeRecommendSubject::getSubjectName,subjectName);
        }
        if (recommendStatus != null){
            queryWrapper.eq(SmsHomeRecommendSubject::getRecommendStatus,recommendStatus);
        }
        Page<SmsHomeRecommendSubject> subjectPage = subjectMapper.selectPage(pageInfo, queryWrapper);
        PageBean pageBean = PageUtils.getPageBean(subjectPage);
        return pageBean;
    }

    @Override
    public int create( List<SmsHomeRecommendSubject> recommendSubjects) {
        for (SmsHomeRecommendSubject recommendSubject : recommendSubjects) {
           subjectMapper.insert(recommendSubject);
        }
        return 1;
       // return subjectMapper.create(recommendSubjects);
    }

    @Override
    public int delete(List<Long> ids) {
        return subjectMapper.deleteBatchIds(ids);
    }

    @Override
    public int updateStatus(List<Long> ids, Integer recommendStatus) {
        List<SmsHomeRecommendSubject> smsHomeRecommendSubjects = subjectMapper.selectBatchIds(ids);
        for (SmsHomeRecommendSubject smsHomeRecommendSubject : smsHomeRecommendSubjects) {
            smsHomeRecommendSubject.setRecommendStatus(recommendStatus);
            subjectMapper.updateById(smsHomeRecommendSubject);
        }
        return 1;
    }

    @Override
    public int updateSort(Integer sort, Integer id) {
        SmsHomeRecommendSubject smsHomeRecommendSubject = subjectMapper.selectById(id);
        smsHomeRecommendSubject.setSort(sort);
        return subjectMapper.updateById(smsHomeRecommendSubject);
    }
}
